﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EvidenceLib.AF;
using EvidenceLib.System;
using EvidenceService.AF;
using EvidenceService.Core;
using EvidenceWeb.Util;
using NHibernate;

namespace EvidenceWeb.Controllers
{
    public class AttachmentController : Controller
    {
        //
        // GET: /Attachment/

        public ActionResult Index()
        {
            return View();
        }


        public ActionResult AttachmentView()
        {
            return View();
        }

        public ActionResult LoadAttachmentList()
        {
            using (ISession session = HbmSessionFactory.GetSession())
            {
                try
                {
                    int CaseId = int.Parse(Request["CaseId"]);
                    int billType = int.Parse(Request["billType"]);
                    int billId = int.Parse(Request["billId"]);

                    AttachmentService svr = new AttachmentService(session);

                    IList<Attachment> attachs = svr.LoadByHQL(string.Format("from Attachment where CaseId ={0} order by BillType", CaseId));

                    string json = JsonSerializerUtil.EntityToJson(attachs);

                    return Json(new { IsSuccess = true, Data = json });
                }
                catch (Exception ex)
                {
                    return Json(new { IsSuccess = false, Data = ex.Message });
                }
            }
        }

        /// <summary>
        /// 文件下载
        /// </summary>
        /// <returns></returns>
        public FileResult FileDownLoad()
        {
            string url = Request["url"];
            string name = Request["name"];

            if (string.IsNullOrEmpty(url))
            {
                return null;
            }
            string path = Server.MapPath(url);

            if (System.IO.File.Exists(path))
            {
                return File(path, "application/octet-stream", Url.Encode(name));
            }
            else
            {
                return null;
            } 
        }

        public ActionResult DeleteAttachment()
        {
            int attachmentId = int.Parse(Request["attachmentId"]);



            using (ISession session = HbmSessionFactory.GetSession())
            {
                

                User user = this.Session["User"] as User;

                if (user == null)
                {
                    return Json(new { IsSuccess = false, Data = "操作已超时，请重新登录!" });
                }
                else
                {


                    Attachment ath = session.Load<Attachment>(attachmentId);

                    if (ath.Submitter.Id != user.Id)
                    {
                        return Json(new { IsSuccess = false, Data = "提交人： "+ ath.Submitter.Name +" 才能删除文件！" });
                    }

                    if (ath == null)
                    {
                        return Json(new { IsSuccess = false, Data = "文件不存在，或者已被删除!" });
                    }
                    else
                    {
                        session.CreateSQLQuery("delete Ev_Attachment where id=" + attachmentId).ExecuteUpdate();

                        string attachmentPath = ConfigurationManager.AppSettings["AttachmentDirectory"];

                        string filePath = Server.MapPath(attachmentPath) + (int)ath.BillType + "/" + ath.BillId + "/" + ath.Name;


                        if (System.IO.File.Exists(filePath))
                        {
                            System.IO.File.Delete(filePath);
                        }

                        return Json(new { IsSuccess = true, Data = "文件删除成功!" });
                    }
                } 

            }

        }

    }

}
